<?php

$act=$_GET["act"];
$idkh=rand(0,999);
if(!session_is_registered("idkh")){
	session_register("idkh");
	$_SESSION["idkh"]=$idkh;
}else{
	$id=$_SESSION["idkh"];
}

if($act=="buy"){
		$id=$_SESSION["idkh"];
		$masp=$_GET["masp"];
		
		$rs=mysql_query("select * from cart where masp='$masp' && idkh='$id'");
		$rs1=mysql_query("select masp,giaban from product where masp='$masp'");
		mysql_num_rows($rs);
		$row=mysql_fetch_row($rs1);
		
		    if(mysql_num_rows($rs)==0){
				$masp=$row[0];
				$gia=$row[1];
				$soluong=1;
				$tongtien=$gia*$soluong;
				
				$ngay=date('Y-m-d h:m:s');
				$rs2=mysql_query("insert into cart values('$masp',$gia,$soluong,$tongtien,'$ngay','$id')");
				echo "<script language=\"javascript1.2\"> location.href=\"index.php?page=detailcart\"</script>";
				//echo $id;
			}else{
				$rows=mysql_fetch_row($rs);
				$sl=$rows[2]+1;
				$tongtien=$rows[1]*$sl;
				$rs2=mysql_query("update cart set soluong='$sl',tongtien='$tongtien' where masp='$masp' && idkh='$id'");
				echo "<script language=\"javascript1.2\"> location.href=\"index.php?page=detailcart\"</script>";
				//echo $id;
			}
			
		}elseif($act=="update"){
			$id=$_SESSION["idkh"];
			$sl= $_POST["soluong"];
			$masp=$_POST["masp"];
			$rs1=mysql_query("select gia from cart where masp='$masp'");
			$row=mysql_fetch_row($rs1);
			$tong=$row[0]*$sl;
			$rs2=mysql_query("update cart set soluong='$sl',tongtien='$tong' where masp='$masp' && idkh='$id'");
			echo "<script language=\"javascript1.2\"> location.href=\"index.php?page=detailcart\"</script>";
			
		}elseif($act=="delsp"){
			 $id=$_SESSION["idkh"];
			 $masp=$_GET["masp"];
			 $xoa=mysql_query("delete from cart where masp='$masp' && idkh='$id'");
			 echo "<script language=\"javascript1.2\"> location.href=\"index.php?page=detailcart\"</script>";
			
		}elseif($act=='delcart'){
			$id=$_SESSION["idkh"];
			$sql=mysql_query("delete from cart where idkh='$id'");
			echo "<script language=\"javascript1.2\"> location.href=\"index.php?page=detailcart\"</script>";
			session_unregister("idkh");
		}


?>